
def get_46words():
    '''
      解析并获取四六级单词列表
    '''
    words = dict()

    with open('46words.txt', 'r',encoding='utf-8') as f:
        for line in f:
            if line and not line == '\n' and not line.startswith('#'):
                line = line.strip('\n')
                level = 'L4'
                if line.startswith('*'):
                    level = 'L6'
                    line = line.lstrip('*')
                if line.startswith('★'):
                    level = 'L6+'
                    line = line.lstrip('★')
                line_words = line.split(' ')
                for w in line_words:
                    w_root = '' if w == line_words[0] else line_words[0]
                    if '/-' in w:
                        w_list = w.split('/-')
                        w1 = w_list[0]
                        w2 = w1[:len(w1)-len(w_list[1])] + w_list[1]
                        words[w1] = set((level,))
                        words[w2] = set((level,))
                    else:
                        if '/' in w:
                            w_list = w.split('/')
                            words[w_list[0]] = set((level,))
                            words[w_list[1]] = set((level,))
                        else:
                            words[w] = set((level,))

    return words

def get_words(w_file_name, default_v=[]):
    words = dict()
    with open(w_file_name, 'r', encoding = 'utf-8') as f:
        for line in f: 
            if line and not line == '\n' and not line.startswith('#'):
                line_words = line.strip('\n').strip().split(',')
                if len(line_words) == 1:
                    words[line_words[0]] = set(default_v)
                else:
                    words[line_words[0]] = set(default_v + line_words[1:])

    return words


def build_my_words():

    words46 = get_46words()
    mc = get_words('mm.txt', ['g'])
    sb = get_words('mysb.txt', ['g', 'sb'])

    all_words = dict()
    for (k, v) in words46.items():
        #s = all_words.get(k, set([]))
        #s.add(v)
        all_words[k] = v

    for (k, v) in mc.items():
        s = all_words.get(k, set())
        s.update(v)
        all_words[k] = s

    for (k, v) in sb.items():
        s = all_words.get(k, set())
        s.update(v)
        all_words[k] = s
    
    return all_words


if __name__ == "__main__":

    all_words = build_my_words()

    # 已经学过的
    got = [k for (k,v) in all_words.items() if 'g' in v]
    # 未学过的 L4
    L4 = [k for (k,v) in all_words.items() if not 'g' in v and 'L4' in v]
    # 未学过的 L6
    L6 = [k for (k,v) in all_words.items() if not 'g' in v and ('L6' in v or 'L6+' in v)]

    print("已学过的单词数： %d\n" % len(got))
    print("未学过的四级单词数： %d\n" % len(L4))
    print(', '.join(L4))
    print('\n')
    print("未学过的六级单词数： %d\n" % len(L6))
    print(', '.join(L4))
